package com.quanyue.music.file.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.music.domain.constant.UserRole;
import com.quanyue.music.file.service.FMFileService;
import com.quanyue.music.file.type.*;
import com.shzisg.mybatis.mapper.page.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Api(tags = "FM文件管理")
@RestController
@RequestMapping("/api")
public class FMFileController {
    @Autowired
    private FMFileService fmFileService;

    @ApiOperation(value = "上传文件")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/fm/file/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> uploadFile(@RequestBody FMUpload fmUpload) {
        fmFileService.upload(fmUpload);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改FM信息")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/fm/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> update(@RequestBody FMUpload fmUpload) {
        fmFileService.update(fmUpload);
        return ServiceResult.SUCCESS;
    }


    @ApiOperation(value = "更新FM喜欢人数")
    @Scope(role = UserRole.USER)
    @PostMapping(value = "/fm/like/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> updateLikeCount(@RequestBody FMLikeRequest request,
                                                   Principal principal) {
        return ServiceResult.success(fmFileService.updateLikeCount(request, principal.getId()));
    }

    @ApiOperation(value = "删除FM信息")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/fm/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> delete(@RequestParam("id") String id) {
        fmFileService.delete(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "FM文件列表")
    @PostMapping(value = "/fm/file/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <FMDetailList>> findList(@RequestBody FMDetailRequest request) {
        return ServiceResult.success(fmFileService.findList(request));
    }

    @ApiOperation(value = "FM文件导出")
    @PostMapping(value = "/fm/file/list/excel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <String> fmExcel(@RequestBody FMListExcel request) {
        return ServiceResult.success(fmFileService.findListExcel(request));
    }

    @ApiOperation(value = "查询紧急文件")
    @GetMapping(value = "/fm/file/sos", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <FMUpload> findSOS() {
        return ServiceResult.success(fmFileService.findSOS());
    }
}
